<!--
 * @Author: XieQiu 雪球
 * @Date: 2021-05-10 15:06:59
 * @LastEditTime: 2023-07-10 11:24:40
 * @LastEditors: Please set LastEditors
 * @Description: 历史巡查记录页面
-->
<template>
  <BaseMainSlot :show-btn-box="false">
    <template v-slot:search-box>
      <SearchForm :formConfig="formConfig" :formData="searchlist" @search="search"></SearchForm>
    </template>
    <template v-slot:table-box>
      <el-table
        ref="tableData"
        v-loading="loading"
        :data="tableData"
        stripe
        border
        height="88%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="index"
          label="序号"
          width="50"
        />
        <el-table-column
          prop="inspectionTaskName"
          label="巡查任务"
        />
        <el-table-column
          prop="inspectionRouteName"
          label="巡查线路"
        />
        <el-table-column
          prop="employeeName"
          label="巡查人"
        />
        <el-table-column
          prop="generateTime"
          label="巡查时间"
          min-width="100"
        >
          <template
            v-if="scope.row.organizationNo"
            slot-scope="scope"
          >
            {{ scope.row.generateTime ? dateToFormat(scope.row.generateTime) : "无"
            }}
            ~ {{ scope.row.endTime ? dateToFormat(scope.row.endTime) : "无" }}
          </template>
        </el-table-column>
        <el-table-column label="巡查结果">
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.inspectionTaskId"
              type="text"
              size="mini"
              @click="showResult(scope.row.inspectionTaskId)"
            >查看巡查结果</el-button>
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :page-size="pageSize"
        :current-page="currentPage"
        :total-size="totalSize"
        @changePageSize="changePageSize"
        @changeCurrentPage="changeCurrentPage"
      />
    </template>
    <template v-slot:dialog>
      <!-- 查询结果数据 -->
      <el-dialog
        class="dialog"
        title="查询结果"
        :visible.sync="isShowResult"
        width="60%"
        center
      >
        <!-- <div class="searchbox">
          <div class="common-search-item">
            <div class="label">巡查路线:</div>
            <div class="input">
              <el-select v-model="inspectionRouteId" filterable clearable>
                <el-option
                  v-for="item in allLineList"
                  :key="item.inspectionRouteId"
                  :label="item.inspectionRouteName"
                  :value="item.inspectionRouteId"
                />
              </el-select>
            </div>
          </div>
          <div class="search ml-20">
            <img src="~@/assets/action-bar-icon/search.png" @click="searchs" />
          </div>
        </div>-->
        <div class="result">
          <el-table
            v-loading="resultLoading"
            highlight-current-row
            :data="resultData"
            border
          >
            <el-table-column
              prop="inspectionPointName"
              label="巡查点"
            />
            <el-table-column
              prop="facilityName"
              label="设施"
            />
            <el-table-column
              prop="alarmBatchName"
              label="批次"
            />
            <el-table-column
              prop="result"
              label="巡查结果"
            >
              <template slot-scope="scope">
                <span v-if="scope.row.finalStatus === 1">正常</span>
                <span v-if="scope.row.finalStatus === 2">异常</span>
              </template>
            </el-table-column>
            <el-table-column
              prop="remark"
              label="结果描述"
            />
            <el-table-column label="查看图片">
              <template slot-scope="scope">
                <el-button
                  v-if="scope.row.finalStatus === 1"
                  type="text"
                  size="mini"
                  @click="showResultmessage(scope.row.pictureId)"
                >查看</el-button>
                <el-button
                  v-if="scope.row.finalStatus === 2"
                  type="text"
                  style="color:#a49090"
                  disabled
                >查看</el-button>
              </template>
            </el-table-column>
            <el-table-column
              prop="alarmDes"
              :label="farmingTypeTitles + '信息'"
            />
            <el-table-column label="查看异常图片">
              <template slot-scope="scope">
                <el-button
                  v-if="scope.row.finalStatus === 2"
                  type="text"
                  size="mini"
                  @click="showResultmessage(scope.row.pictureId)"
                >查看</el-button>
                <el-button
                  v-if="scope.row.finalStatus === 1"
                  type="text"
                  disabled
                  style="color:#a49090"
                >查看</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-dialog>
      <!-- 设施批次图片信息
      <el-dialog
        class="dialog"
        title="查询结果"
        :visible.sync="isShowResultmessage"
        width="60%"
        center
      >
        <div class="result">
          <el-table highlight-current-row :data="resultDatatwo" stripe border>
            <el-table-column prop="facilityName" label="设施" />
            <el-table-column prop="batchName" label="批次" />
            <el-table-column prop="pictureId" label="图片" />
            <el-table-column label="图片展示">
              <template slot-scope="scope">
                <el-button
                  type="primary"
                  size="mini"
                  @click="getPictureMessage(scope.row.pictureId)"
                >
                  查看图片
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-dialog>-->

      <el-dialog
        class="dialog dialogIMG"
        :visible.sync="isShowimage"
        width="60%"
        center
      >
        <img
          v-for="item in imageUrlList"
          :key="item.pictureId"
          class="imgmessage"
          width="100%"
          :src="'data:image/jpg;base64,' + item.pictureData"
        />
        <!-- </div> -->
      </el-dialog>
    </template>
  </BaseMainSlot>
</template>

<script>
// import ActionBar from "@/components/ActionBar";
import SearchForm from '@/components/searchForm';
import Pagination from "@/components/Pagination";
import patrolInfoManage from "@/api/digital-operation/patrol-info-manage/history-patrol-record";
import staffManage from "@/api/digital-operation/personnel-cost-mansge/staff-manage";
// import collectionPicture from "@/api/digital-production/collectionPicture/index";
import BaseMainSlot from "@/components/BaseMainSlot/newBaseMainSlot.vue";
import StartTimeEndTimeSelectionBox from "@/components/StartTimeEndTimeSelectionBox";
import { mymixin } from "@/utils/mixin";
import { farmingTypeTitle } from "@/utils/validate";
export default {
  name: "HistoryPatrolRecord",
  components: {
    SearchForm,
    Pagination,
    BaseMainSlot,
    StartTimeEndTimeSelectionBox,
  },
  mixins: [mymixin],
  data() {
    return {
      formConfig: [
        {
          label: '巡查任务名称',
          prop: 'inspectionTaskName',
          type: 'input',
        }, {
          label: '巡查路线',
          prop: 'inspectionRouteId',
          type: 'select',
          options: [], // allLineList
          optionKey: { label: 'inspectionRouteName', value: 'inspectionRouteId' },
        }, {
          label: '巡查人',
          prop: 'employeeName',
          type: 'select',
          options: [], // staffData
          optionKey: { label: 'personName', value: 'personName' },
        }, {
          label: '巡查时间',
          prop: 'date',
          type: 'daterange',
        }, 
      ],
      searchlist: {},
      // 巡检点名称
      RouteName: "",
      farmingTypeTitles: farmingTypeTitle(),
      // 选中的数据项数组 ID组成
      inspectionTaskIdList: [],
      // 是否显示巡查结果
      isShowResult: false,
      // 显示设施批次图片详细信息
      isShowResultmessage: false,
      // 展示图片
      isShowimage: false,
      // 设施批次详细信息列表
      resultDatatwo: [],
      // 图片地址
      nowimageurl: "",
      resultLoading: true,
      resultData: [],
      // 所有巡查人列表
      allStaffList: [],
      // 所有巡查路线列表
      allLineList: [],
      // search值
      // 时间间隔
      rangeTime: "",
      // 巡查路线ID
      inspectionRouteId: "",
      // 巡查人ID
      employeeName: "",
      staffData: [],
      dataTimeStart: "",
      inspectionTaskName: "",
      dataTimeEnd: "",
      // 图片
      imageUrlList: [],
    };
  },
  watch: {},
  created() {
    this.queryDataList();
  },
  mounted() {
    // 获取表格数据
    this.getTableDataList();
  },
  methods: {
    getTableDataList() {
      this.tableData = [];
      this.loading = true;
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {
          // organizationNo: sessionStorage.organizationNo,
          inspectionRouteId: this.searchlist.inspectionRouteId || undefined,
          employeeName: this.searchlist.employeeName || undefined,
          inspectionTaskName: this.searchlist.inspectionTaskName || undefined,
          dataTimeStart : this.searchlist.date && this.dateToFormattwo(this.searchlist.date[0]),
          dataTimeEnd: this.searchlist.date && this.dateToFormattwo(this.searchlist.date[1]),
          beforeFlagList: [2, 3],
          checkFlag: true,
          organizationNo: sessionStorage.organizationNo
        }
      };
      patrolInfoManage.getInspectionTaskList(params).then(res => {
        this.loading = false;
        let data = res.data;
        // 赋值总条数
        if (data) {
          this.totalSize = data.totalElements;
          // 赋值数组
          this.tableData = data.content;
          this.addblankmet();
          this.resetActionBar();
        }
      });
    },
    // 巡查人
    async queryDataList() {
      const { data: Staff } = await staffManage.getStaffListByCondition();
      // 赋值数组
      this.staffData = Staff;
      let list = Staff.map(el => {
        return { personName: el.person.name, personNo: el.person.personNo }
      })
      this.setOptions(this.formConfig, 'employeeName', list)
      const {
        data: inspectionRoute
      } = await patrolInfoManage.inspectionRouteCondition();
      this.allLineList = inspectionRoute;
      this.setOptions(this.formConfig, 'inspectionRouteId', inspectionRoute)
    },
    // 时间转换
    dateFormat(row, colnum) {
      if (row.generateTime) {
        return this.$moment(row[colnum.property]).format("YYYY-MM-DD HH:mm:ss");
      }
      if (row.endTime) {
        return this.$moment(row[colnum.property]).format("YYYY-MM-DD HH:mm:ss");
      }
    },
    search() {
      // let time1 = this.$moment(this.rangeTime[0]).format("YYYY-MM-DD HH:mm:ss");
      this.currentPage = 1;
      this.getTableDataList();
    },
    searchs() {
      // let time1 = this.$moment(this.rangeTime[0]).format("YYYY-MM-DD HH:mm:ss");
      this.currentPage = 1;
      this.showResult();
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.getTableDataList();
    },
    changeCurrentPage(currentPage) {
      this.currentPage = currentPage;
      this.getTableDataList();
    },
    handleEdit(row) {
      // console.log(row);
      // TODO 点击行元素后 edit
      this.formData = JSON.parse(JSON.stringify(row));
      // this.$refs.formData.resetFields();
      // console.log(this.formData)
    },
    handleSelectionChange(list) {
      this.inspectionTaskIdList = [];
      this.inspectionTaskIdList = list.map(item => item.inspectionTaskId);
      if (this.inspectionTaskIdList.length > 0) this.delActive = true;
      else this.delActive = false;
    },
    showResultmessage(Id) {
      let params = {
        pictureId: Id
      };
      // console.log(params)
      patrolInfoManage.queryByConditionList(params).then(res => {
        // console.log(res)
        let data = res.data;
        this.imageUrlList = data;
        // console.log(this.imageUrlList)
        // this.nowimageurl = "data:image/jpg;base64," + res.d ata.pictureData;
        // console.log(this.nowimageurl);
        if (data.length > 0) this.isShowimage = true;
        else this.$message.warning("暂无图片");
      });
      // patrolInfoManage.getResultByTaskResultId(taskResultId).then(res => {
      //   this.resultDatatwo = res.data.taskResultInfoPageList;
      // });
    },
    getPictureMessage(collectionPictureId) {
      this.isShowimage = true;
      patrolInfoManage.getPictureMessage(collectionPictureId).then(res => {
        this.nowimageurl = `data:image/png;base64,${res.agriculturalMaterialPicture.picture}`;
        // this.nowimageurl = res;
      });
    },
    // 获取结果
    showResult(inspectionTaskId) {
      if (inspectionTaskId) {
        this.isShowResult = true;
        if (inspectionTaskId) {
          let params = {
            inspectionTaskId: inspectionTaskId
          };
          patrolInfoManage.getInspectionResultList(params).then(res => {
            this.resultLoading = false;
            this.resultData = res.data;
            // console.log(this.resultData);
          });
        }
      } else {
        return;
      }
    },
    delmessage() {
      patrolInfoManage
        .delInspectionRouteInfo(this.inspectionTaskIdList)
        .then(res => {
          if (res.code === 1) {
            this.$message.success(res.msg);
            this.getTableDataList();
          } else {
            this.$message.error("删除失败");
          }
        });
    }
  }
};
</script>

<style scoped lang="scss">
@import "~@/styles/variables.scss";
.common-layout {
  border: 1px solid $borderColor;
  height: calc(100% - #{$actionBarHeight + 10});
  padding: 10px;
  .search-box {
    display: flex;
    align-items: center;
    .label-input {
      display: flex;
      align-items: center;
      .label {
        min-width: 80px !important;
        white-space: nowrap;
      }
      // .label-80 {
      //   // width: 80px;
      // }
    }
    .search {
      display: flex;
      align-items: center;
      img {
        height: 30px;
      }
    }
  }
  .content {
    height: calc(100% - 50px);
    margin: 20px 0;
    .table-box {
      position: relative;
      height: 100%;
      overflow-y: auto;
      .el-table {
        width: 100%;
      }
    }
  }
}
// }
::v-deep .el-dialog {
  overflow-y: auto;
  // background-color: rgba($color: #82ceee, $alpha: 0.8);
  .el-form {
    display: flex;
    .el-form-item {
      display: flex;
    }
  }
  .searchbox {
    display: flex;
    align-items: center;
    margin-bottom: 10px;
    .label-input {
      color: cornsilk;
      display: flex;
      align-items: center;
      .label {
        margin-right: 2px;
      }
    }
  }
  .el-table__empty-text {
    color: #000 !important;
  }
  .el-dialog__body {
    text-align: initial;
    padding: 20px 10px 10px !important;
  }
}

::v-deep .el-dialog {
  border: 1px solid #000;
  border-radius: 10px;
  .el-dialog__header {
    border-radius: 9px 9px 0 0;
    .el-dialog__title {
      color: #fff;
    }
  }
  .el-dialog__body {
    padding: 5px 20px;
    font-size: 12px !important;
    .el-input {
      input {
        font-size: 12px;
      }
    }
  }
  .el-table {
    max-height: 200px;
    overflow-y: auto;
    background-color: rgba(255, 255, 255, 1);
    .cell {
      color: #000;
    }
    th {
      background-color: rgba(230, 230, 230, 1);
    }
    th.is-leaf {
      border-color: rgba(77, 77, 77, 1);
    }
    td {
      border-color: rgba(77, 77, 77, 1);
    }
    tr:hover {
      background-color: rgba(77, 77, 77, 1);
    }
    .el-table__empty-text {
      color: #000;
    }
    .current-row {
      // background-color: rgb(177, 177, 177);
      background-color: #dbdbdb !important;
    }
    .el-table__row--striped {
      background-color: rgba(255, 255, 255, 1);
    }
    // color: #000;
  }
  .el-table--border {
    border-color: rgba(77, 77, 77, 1);
    border-top-left-radius: 8px;
    border-top-right-radius: 8px;
  }
  .el-table--enable-row-hover .el-table__body tr:hover > td {
    background-color: rgb(177, 177, 177);
  }
  .el-table__body tr.current-row > td {
    background-color: #dbdbdb !important;
  }
  .el-form-item {
    margin-bottom: 10px;
  }
  .total-price {
    color: #000;
  }
  .el-table__body {
    // height: ;
    height: 0;
    // background-color: #003947;
  }
}
::v-deep .dialogIMG {
  .el-dialog {
    height: 80%;
    .el-dialog__header {
      padding: 30px;
    }
    .el-dialog__body {
      height: 92%;
      overflow-y: auto !important;
    }
  }
}
</style>
